﻿//-----------------------------------------------------------------------
// <copyright file="BusinessNullRule.cs" company="FastNET Services">
//     Copyright (c) 2007-2013. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Reflection;

#endregion

namespace FastNet.BusinessSystem.Rules
{
    /// <summary>
    /// Business rule for checking for a null value on a value type
    /// </summary>
    public sealed class BusinessNullValueRule<TRange> : BusinessRule where TRange : struct
    {
        #region Variables

        private readonly Func<TRange?> _validateMethod;

        #endregion

        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the BusinessNullValueRule class
        /// </summary>
        /// <param name="ruleName">Name of the rule</param>
        /// <param name="propertyName">Name of the property the rule validates</param>
        /// <param name="errorMessage">Message to display when in error</param>
        /// <param name="validateMethod">Method/Lambda to call when validating rule</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public BusinessNullValueRule(string ruleName, string propertyName, string errorMessage, Func<TRange?> validateMethod)
            : base(ruleName, propertyName, errorMessage)
        {
            _validateMethod = validateMethod;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Called to check the validity of the rule
        /// </summary>
        /// <returns>Boolean whether or not the rule is valid</returns>
        public override bool IsValid()
        {
            return _validateMethod().HasValue;
        }

        #endregion
    }


    /// <summary>
    /// Business rule for checking for a null reference
    /// </summary>
    public sealed class BusinessNullReferenceRule<TRange> : BusinessRule where TRange : class
    {
        #region Variables

        private readonly Func<TRange> _validateMethod;

        #endregion

        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the BusinessNullReferenceRule class
        /// </summary>
        /// <param name="ruleName">Name of the rule</param>
        /// <param name="propertyName">Name of the property the rule validates</param>
        /// <param name="errorMessage">Message to display when in error</param>
        /// <param name="validateMethod">Method/Lambda to call when validating rule</param>
        public BusinessNullReferenceRule(string ruleName, string propertyName, string errorMessage, Func<TRange> validateMethod)
            : base(ruleName, propertyName, errorMessage)
        {
            _validateMethod = validateMethod;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Called to check the validity of the rule
        /// </summary>
        /// <returns>Boolean whether or not the rule is valid</returns>
        public override bool IsValid()
        {
            return _validateMethod() == null;
        }

        #endregion
    }
}
